﻿using ORM.Applicaiton;
using ORM.Util;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ORM.Parser.Design
{
    public class UpdateStatement : BaseStatement
    {
        public string Expression { set; get; }
        public List<string> UpdateColumns { set; get; }
        public DataTable OriginalData { set; get; }
        public override void Parse(string SQLText)
        {
            base.Parse(SQLText);
            UpdateColumns = new List<string>();
            DBUtil dbutil = new DBUtil();
            int updateindex = this.SQLText.IndexOf("update\n");
            int whereindex = this.SQLText.IndexOf("where\n");
            int setindex = this.SQLText.IndexOf("set\n");
            this.TableName = this.SQLText.Substring(updateindex + 8, setindex - updateindex - 8).Trim();
            PKName = MapperBuilder.GetInstance().GetSchemaTableKeyFieldName(ConnectStringName, TableName).ToLower();
            this.Expression = this.SQLText.Substring(whereindex + 6, this.SQLText.Length - whereindex - 6).Trim();
            string[] columns = this.SQLText.Substring(setindex + 4, this.SQLText.Length - setindex - 4).Replace("where\n", string.Empty).Replace(Expression, string.Empty).Trim().Split(new char[] { ',' }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var item in columns)
            {
                UpdateColumns.Add(item.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries)[0].Trim());
            }
            StringBuilder orignalsqlbuilder = new StringBuilder();
            orignalsqlbuilder.AppendFormat("select {0},", PKName);
            foreach (string item in UpdateColumns)
            {
                if (UpdateColumns.IndexOf(item) == UpdateColumns.Count - 1)
                {
                    orignalsqlbuilder.Append(item);
                }
                else
                {
                    orignalsqlbuilder.Append(item + ",");
                }

            }
            orignalsqlbuilder.Append(string.Format(" from {0} where {1}", TableName, Expression));
            OriginalData = dbutil.FindView(orignalsqlbuilder.ToString(), ConnectStringName);

        }

        public override List<string> GetRollBackSQL()
        {
            foreach (DataRow item in OriginalData.Rows)
            {
                StringBuilder rollbackbuilder = new StringBuilder();
                rollbackbuilder.Append(string.Format("update {0} set ", TableName));
                for (int i = 0; i < UpdateColumns.Count; i++)
                {
                    rollbackbuilder.Append(string.Format("{0}='{1}'", UpdateColumns[i], Convert.ToString(item[UpdateColumns[i]])));
                    if (i != UpdateColumns.Count - 1)
                    {
                        rollbackbuilder.AppendFormat(",");
                    }
                }
                rollbackbuilder.Append(string.Format(" where {0}='{1}'", PKName, Convert.ToString(item[PKName])));
                this.RollBackSQLTextList.Add(rollbackbuilder.ToString());

            }
            return base.GetRollBackSQL();
        }
    }
}
